//封装JS的Date，提供时间相关的服务
import promptAction from '@ohos.promptAction';
import { MyDate } from './MyDate';
export abstract class MyTime {
  public static HOURS:string[] = ['06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '00', '01', '02', '03', '04', '05']

  //Date转hh:mm字符串
  public static date2hhmm(date:Date):string {
    const hh = String(date.getHours()).padStart(2, '0');  //获取当前小时数(0-23)
    const mm = String(date.getMinutes()).padStart(2, '0');  //获取当前分钟数(0-59)
    return hh + ':' + mm
  }

  //获得当前时间
  public static now():string {
    return MyTime.date2hhmm(new Date())
  }

  //根据触摸位置获得时间
  public static position2hhmm(y:number):string {
    y = Math.max(y, 0)
    y = Math.min(y, 23 * 60 + 59)
    const hh = MyTime.HOURS[Math.floor(y / 60)]
    const mm = String(Math.floor(y % 60)).padStart(2, '0')
    return hh + ':' + mm
  }

  //根据触摸位置决定时间是在该天还是在下一天
  public static position2yyyyMMDD(date:string,y:number):string {
    if(y / 60 < 18) {
      return date
    } else {
      return MyDate.dateWithOffset(date, 1)
    }
  }

  //根据时间获得位置
  public static hhmm2position(time:string):number {
    const hours = MyTime.getHours(time)
    const minutes = MyTime.getMinutes(time)
    const index = hours >= 6 ? hours - 6 : hours + 18
    return index * 60 + minutes
  }

  public static getHours(time:string):number {
    return Number(time.split(':')[0])
  }

  public static getMinutes(time:string):number {
    return Number(time.split(':')[1])
  }

  //start到end的时间间隔，单位是分钟
  public static getDuration(start:string, end:string):number {
    return 60 * (MyTime.getHours(end) - MyTime.getHours(start)) + MyTime.getMinutes(end) - MyTime.getMinutes(start)
  }

  //时间加上一个偏移量(以分钟为单位)，不超过今天
  public static timeWithOffset(time:string, offset:number):string {
    const hours = MyTime.getHours(time)
    const minutes = MyTime.getMinutes(time)
    var totalMinutes:number = hours * 60 + minutes + offset
    totalMinutes = Math.max(totalMinutes, 0)
    totalMinutes = Math.min(totalMinutes, 23 * 60 + 59)
    return Math.floor(totalMinutes/60).toString().padStart(2, '0') + ':' + Math.floor(totalMinutes%60).toString().padStart(2, '0')
  }

}